{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "29bb608c-3dc3-4556-b4d2-58e078d56ef6",
   "metadata": {},
   "source": [
    "### 条件与循环的使用    \n",
    "l =[1,2,3,4,5,6] \n",
    "for index in range(0,len(l)):\n",
    "    if index <5: \n",
    "        print(l[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "219a3035-f631-4046-bd73-9ab20498e6e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "l = [1,2,3,4,5,6] \n",
    "for index,item in enumerate(l):\n",
    "    if index < 5: \n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a4706ce-e090-4e4b-9e6f-51d83716413d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环表达式的复用 \n",
    "#expression if condition else expression2 for item in iterable \n",
    "# 表达式 y = 2*|x| +5 \n",
    "y = [value*2 +5 if value >0 else -value *2 +5 for value in x]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b1a6158a-9155-4553-aa87-214d54eb4a1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Today', 'Sunday']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取文本中完整的语句，按照逗号分割单词，去掉首位的空字符，并过滤长度小于等于3的单词\n",
    "text = ' Today, is,Sunday' \n",
    "text_list = [s.strip() for s in text.split(',') if len(s.strip())>3]\n",
    "text_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c26981d9-62dc-49f3-931d-d423872673f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "[(xx,yy) for xx in x for yy in y if xx !=yy] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44523344-88f2-4f8c-b7e3-fc16fef6ae61",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [] \n",
    "for xx in x:\n",
    "    for yy in y:\n",
    "        if xx != yy:\n",
    "            l.append((xx,yy)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bd25c0aa-283e-4dbe-8f8e-adb834a29d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考题 \n",
    "attributes = ['name', 'dob', 'gender']\n",
    "values = [['jason', '2000-01-01', 'male'], ['mike', '1999-01-01', 'male'],['nancy', '2001-02-01', 'female']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fb4266c1-8ce1-4324-9aeb-d30371281a35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'}, {'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'}, {'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]\n"
     ]
    }
   ],
   "source": [
    "# 一行代码\n",
    "print([dict(zip(attributes,value)) for value in values])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6ec7da08-b80b-49c4-b86f-86e45a59fec5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'}, {'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'}, {'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]\n"
     ]
    }
   ],
   "source": [
    "# 多行代码输出 \n",
    "list1 = [] \n",
    "for value in values:\n",
    "    dict1 = {}\n",
    "    for index,item in enumerate(attributes):\n",
    "        dict1[item] = value[index]\n",
    "    list1.append(dict1) \n",
    "print(list1)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5ed4269-4010-474c-93c4-7d227e349a20",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
